package ods_odb

import (
	"fmt"
	"server/global"
	"server/model/common/request"
	"server/model/common/response"

	"github.com/gin-gonic/gin"
	_ "github.com/sijms/go-ora/v2"
	"go.uber.org/zap"
)

type OdsOdbApi struct {
}

var odsOdbService = OdsOdbService{}

func (s *OdsOdbApi) CreateOdsOdb(c *gin.Context) {
	var odsOdb OdsOdb
	err := c.ShouldBindJSON(&odsOdb)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := odsOdbService.CreateOdsOdb(&odsOdb); err != nil {
		global.Logger.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

func (s *OdsOdbApi) DeleteOdsOdb(c *gin.Context) {
	var odsOdb OdsOdb
	err := c.ShouldBindJSON(&odsOdb)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := odsOdbService.DeleteOdsOdb(odsOdb); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

func (s *OdsOdbApi) DeleteOdsOdbByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := odsOdbService.DeleteOdsOdbByIds(IDS); err != nil {
		global.Logger.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

func (s *OdsOdbApi) UpdateOdsOdb(c *gin.Context) {
	var odsOdb OdsOdb
	err := c.ShouldBindJSON(&odsOdb)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := odsOdbService.UpdateOdsOdb(odsOdb); err != nil {
		global.Logger.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

func (s *OdsOdbApi) FindOdsOdb(c *gin.Context) {
	var odsOdb OdsOdb
	err := c.ShouldBindQuery(&odsOdb)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reodsOdb, err := odsOdbService.GetOdsOdbById(odsOdb.ID); err != nil {
		global.Logger.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reodsOdb": reodsOdb}, c)
	}
}

func (s *OdsOdbApi) GetOdsOdbList(c *gin.Context) {
	var pageInfo OdsOdbSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := odsOdbService.GetOdsOdbList(pageInfo); err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (s *OdsOdbApi) GetAllOdsOdb(c *gin.Context) {
	odsOdb, err := odsOdbService.GetAllOdsOdbs()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"odsOdb": odsOdb}, "获取成功", c)
	}
}

func (s *OdsOdbApi) TestOdsOdb(c *gin.Context) {
	var m OdsOdb
	err := c.ShouldBindJSON(&m)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	_, err = m.GetConn()
	if err != nil {
		msg := fmt.Sprintf("获取mysql 连接失败:%v", zap.Error(err))
		global.Logger.Error(msg)
		response.FailWithMessage(msg, c)
	}
	response.OkWithData(m, c)
}
